﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CuaHangLaptop.BIZ;

namespace QuanLy.GUI
{
    public partial class NhaSX_GUI : Form
    {
        List<NhaSX_BIZ> list_NSX;
        NhaSX_BIZ current_NSX = new NhaSX_BIZ();
        bool updated = true;

        public NhaSX_GUI()
        {            
            InitializeComponent();
            GridView_nsx.AutoGenerateColumns = false;
            Frm_Load();
        }

        void Frm_Load()
        {
            list_NSX = current_NSX.GetAll_NSX();
            GridView_nsx.DataSource = list_NSX;
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            current_NSX = new NhaSX_BIZ();
            txt_MaNSX.ReadOnly = txt_TenNSX.ReadOnly = false;
            txt_MaNSX.Text = "";
            txt_TenNSX.Text = "";
            updated = false;
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            try
            {
                if (!updated)
                {
                    current_NSX = new NhaSX_BIZ(-1, txt_MaNSX.Text, txt_TenNSX.Text);
                    list_NSX.Add(current_NSX);
                }
                else
                {
                    current_NSX.TenNSX = txt_TenNSX.Text;
                    current_NSX.MaNSX = txt_MaNSX.Text;
                }
                current_NSX.Save();
                GridView_nsx.DataSource = null;
                GridView_nsx.DataSource = list_NSX;
                txt_MaNSX.ReadOnly = txt_TenNSX.ReadOnly = true;
                updated = true;
            }
            catch
            {
                MessageBox.Show("Lỗi!");
            }            
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GridView_nsx_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (GridView_nsx.Columns[e.ColumnIndex].HeaderText == "Xóa")
            {
                current_NSX = (NhaSX_BIZ)GridView_nsx.Rows[e.RowIndex].DataBoundItem;
                list_NSX.Remove(current_NSX);
                current_NSX.Delete();
                GridView_nsx.DataSource = null;
                GridView_nsx.DataSource = list_NSX;
            }
            if (GridView_nsx.Columns[e.ColumnIndex].HeaderText == "Sửa")
            {
                current_NSX = (NhaSX_BIZ)GridView_nsx.Rows[e.RowIndex].DataBoundItem;
                txt_MaNSX.ReadOnly = txt_TenNSX.ReadOnly = false;
            }
        }

        private void GridView_nsx_SelectionChanged(object sender, EventArgs e)
        {
            if (GridView_nsx.SelectedRows.Count > 0)
            {
                current_NSX = (NhaSX_BIZ)GridView_nsx.SelectedRows[0].DataBoundItem;
                txt_MaNSX.Text = current_NSX.MaNSX;
                txt_TenNSX.Text = current_NSX.TenNSX;
                txt_MaNSX.ReadOnly = txt_TenNSX.ReadOnly = true;
            }
        }
    }
}
